<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <div class="cmt">
        <textarea placeholder="请输入要评论的内容（最多吐槽120字）" maxlength="120" v-model="msg">

        </textarea>
        <mt-button type="primary" size="large" @click="postComments">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="item in comments" :key="item.add_time">
                <div class="cmt-title">
                  <img src="../../lib/images/userIcon.png">
                  <div class="userName">
                    <p>{{item.user_name}}</p>
                    
                    <span class="mui-icon-extra mui-icon-extra-like">赞</span>
                    
                  </div>
                </div>
                <div class="cmt-body">
                    {{item.content==='undefined'?'此用户很懒，嘛都没说':item.content}}
                </div>
                <p class="addTime">发表时间：{{item.add_time|dateFormart}}</p>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
        </div>
    </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      pageindex: 1,
      comments: [],
      msg:""
    };
  },
  created() {
    this.getComments();
  },
  methods: {
    getMore() {
      this.pageindex++;
      this.getComments();
    },
    getComments() {
      //获取用户评论数据
      this.$http
        .get("api/getcomments/" + this.id + "?pageindex=" + this.pageindex)
        .then(result => {
          console.log(result.body);
          if (result.body.status === 0) {
            this.comments = this.comments.concat(result.body.message);
          } else {
            Toast("评论加载失败。。。");
          }
        });
    },
    postComments() {
      //用户发表评论
      //校验发表内容是否为空
      if (this.msg.trim().length === 0) {
        return Toast("评论内容不能为空。。。");
      }

      //参数1：请求的URL地址
      //参数2：提交给服务器的数据对象 {content:this.msg}
      //参数3：定义提交时候 表单中数据的格式 {emulateJSON:true}
      this.$http
        .post("api/postcomment/" + this.$route.params.id, {
          content: this.msg.trim()
        })
        .then(result=> {
            if(result.body.status===0)
            {
                //拼接出一个评论对象
                var cmt={
                  user_name:"匿名用户",
                  add_time:Date.now(),
                  content:this.msg.trim()
                };
                this.comments.unshift(cmt);
                //清空评论框的内容
                this.msg="";
            }else{
              Toast("发表评论失败。。。")
            }
        });
    }
  },
  props: ["id"]
};
</script>
<style lang="scss" scoped>
.cmt-container {
  
  h3 {
    font-size: 18px;
    margin:0 10px;
  }
 .cmt{
  margin:0 10px;
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }

  .cmt-list {
    margin-top: 10px;
    margin-bottom: 10px;
    .cmt-item {
      .addTime{
        padding-left:50px;
        font-size: 12px;
        color:black;
      }
      font-size: 14px;
      padding-bottom: 15px;
      .cmt-title {
        display: flex;
        line-height: 30px;
        img{
          width: 40px;
          height: 40px;
          border-radius: 50%;
        }
        .userName{
          display: flex;
          justify-content: space-between;
          margin-left:10px;
          width: 100%;
          p{
            font-size: 14px;
            color:blue;
          }
          .mui-icon-extra{
            font-size: 14px;
            margin-left: 210px;
            line-height: 30px;
            color: rgb(54, 53, 53);
          }
        }
      }
      .cmt-body {
        line-height: 25px;
        padding-left:50px;
      }
    }
  }
 }
}
</style>


